<%@include file="/WEB-INF/includes/taglibs.jsp"%>
<table class="content">
	<c:set var="runningTotal" scope="request" value="${account.balance}"/>
	<tr>
		<td valign="bottom" width="1%">
			<spring:url value="/accounts/${account.id}/delete" var="urlDelete"/>
       		<a href="${urlDelete}">
       			<spring:url value="/static/images/delete.png" var="imgDelete"/>
       			<img title="Delete account" src="${imgDelete}"/>
       		</a>
		</td>
		<td valign="bottom" width="1%">
			<spring:url value="/account/${account.id}/edit" var="urlEdit"/>
       		<a href="${urlEdit}">
       			<spring:url value="/static/images/update.png" var="imgEdit"/>
       			<img title="Update account" src="${imgEdit}"/>
       		</a>
		</td>
		<td valign="bottom" width="50%">		
			<strong><c:out value="${account.name}"/></strong> 
			<c:if test="${not empty accountTransactions}">
				(<fmt:formatDate value="${firstTransaction.due}" pattern="EEEE, dd MMMM yyyy"/> -  <fmt:formatDate value="${lastTransaction.due}" pattern="EEEE, dd MMMM yyyy"/>)
			</c:if>
		</td>
		<td align="right" width="50%">
			<spring:url var="addAccountTransaction" value="/accounts/${account.id}/accountTransactions/create"/>
			<form id="addAccountTransaction" method="get" action="${addAccountTransaction}">
				<div class="submit">
					<input type="submit" value="Add Item" />
				</div>
			</form>
		</td>		
	</tr>
	<tr>
		<td colspan="4">
			<table class="balance">
				<tr>
					<th width="1%"></th>
					<th width="1%"></th>
					<th width="1%"></th>
					<th width="57%">Item</th>                    
                    <th width="10%">Type</th>                                            
                    <th width="10%">Amount</th>
                    <th width="10%">Due</th>          
                    <th width="10%">Balance</th>					
				</tr>
				<tr>
					<td colspan="3"></td>
                	<td colspan="4"><strong>Current balance</strong></td>
                	<td class="balance editable"><fmt:formatNumber value="${account.balance}" type="currency" currencySymbol="$" /></td>             
                </tr>
                <c:if test="${empty accountTransactions}">
                	<tr>
                		<td colspan="8">There are no items in this account yet. Click 'Add Item' above to add some.</td>
                	</tr>
                </c:if>              
                <c:forEach items="${accountTransactions}" var="accountTransaction">
                    <tr>
                    	<td>
                    		<spring:url value="/accounts/${account.id}/accountTransactions/${accountTransaction.id}/delete" var="urlDelete"/>
                    		<a href="${urlDelete}">
                    			<spring:url value="/static/images/delete.png" var="imgDelete"/>
                    			<img title="Delete item" src="${imgDelete}"/>
                    		</a>
                    	</td>
                    	<td>
                    		<spring:url value="/schedules/${accountTransaction.schedule.id}" var="urlEdit"/>
                    		<a href="${urlEdit}">
                    			<spring:url value="/static/images/update.png" var="imgEdit"/>
                    			<img title="Update item" src="${imgEdit}"/>
                    		</a>
                    	</td>
                    	<td>
                    		<c:choose>                    			
	                    		<c:when test="${accountTransaction.paid}">
	                    			<spring:url value="/static/images/money_grey.png" var="imgAlreadyPaid"/>
	                    			<img title="Item already paid" src="${imgAlreadyPaid}"/>
	                    		</c:when>   
                    			<c:otherwise>
                    				<spring:url value="/accounts/${account.id}/accountTransactions/${accountTransaction.id}/paid" var="urlMarkPaid"/>
		                    		<a class="markPaid" href="${urlMarkPaid}">
		                    			<spring:url value="/static/images/money.png" var="imgMarkPaid"/>		                    			
		                    			<img title="Mark item paid" src="${imgMarkPaid}"/>
		                    		</a>
                    			</c:otherwise>
                    		</c:choose>               		                    		
                    	</td>
                        <td>
                            <c:out value="${accountTransaction.schedule.name}"/>
                        </td>
                        <td class="itemType">
                            <c:out value="${accountTransaction.schedule.transactionType}"/>
                        </td>
                        <td>
                            <fmt:formatNumber value="${accountTransaction.schedule.amount}" type="currency" currencySymbol="$" /> 
                        </td>
                        <td>
                            <fmt:formatDate pattern="dd MMM yyyy" value="${accountTransaction.due}"/>
                        </td>                                   
                        <td class="balance">
                        	<c:set var="runningTotal" scope="request" value="${runningTotal + accountTransaction.schedule.amount}"/>                            
                            <fmt:formatNumber value="${runningTotal}" type="currency" currencySymbol="$" />                            
                        </td>        
                    </tr>
                </c:forEach>
			</table>
		</td>
	</tr>
</table>